// #ifdef MP-WEIXIN
import TIMSDK from './sdk/tim-sdk/tim-wx.js'
// #endif
//  #ifndef MP-WEIXIN
import TIMSDK from './sdk/tim-sdk/index.js'
//  #endif 
import TIMUploadPlugin from './sdk/tim-upload-plugin/index.js'
import store from '@/store/index.js'
export default {
    install: function(Vue){
		const $tim = {
			sdk:TIMSDK,
			tim:TIMSDK.create({
				SDKAppID: 1400547331
			})
		}
		$tim.tim.registerPlugin({
			'tim-upload-plugin':TIMUploadPlugin
		})
		$tim.tim.setLogLevel(1);
		
		$tim.timMessageReceived = (event) =>{
			if (event.name == "onMessageReceived") {
				event.data.map(item=>{
					store.commit('tim/SET_timMsg',item);
					// if(item.payload.data === "客服切换"){
					// 	this.getConnect();
					// }else{
					// 	if(item.from == "administrator" || item.from==this.timData.to){
					// 		this.messages.push(item)
					// 	}
					// }
				})
			}
		}
		
		$tim.login = (userID="",userSig="") => {
			let timInfo = uni.getStorageSync('timInfo');
			if(timInfo){
				!userID && (userID = timInfo.userID);
				!userSig && (userSig = timInfo.userSig);
			}
			if(!userID || !userSig){
				return
			}
			let promise = $tim.tim.login({
				userID,
				userSig
			});
			promise.then((imResponse) => {
				store.commit('tim/SET_timStatus',true);
				// this.createTempMsg(this.timData.kefu_info_name,`{"chat_id":${this.chat_id},"format":"system"}`,this.getUuid())
				if (imResponse.data.repeatLogin === true) {
					// 标识账号已登录，本次登录操作为重复登录。v2.5.1 起支持
					console.log(imResponse.data.errorInfo);
				}
				// this.messages = [];
				$tim.tim.on(TIMSDK.EVENT.MESSAGE_RECEIVED, $tim.timMessageReceived);
			}).catch((imError)=> {
				store.commit('tim/SET_timStatus',false);
				console.warn('login error:', imError); // 登录失败的相关信息
			});
		}
		$tim.logout = () => {
			let promise = $tim.tim.logout();
			promise.then(function(imResponse) {
			  console.log(imResponse.data); // 登出成功
			  store.commit('tim/SET_timStatus',false);
			  uni.removeStorageSync('timInfo')
			}).catch(function(imError) {
			  console.warn('logout error:', imError);
			});
		}
		Vue.prototype.$tim = $tim;
    }
}